﻿using Beetle;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;

internal class Class_t : Attribute
{
    private Queue<Class_a> _field_a;
    private Class_a _field_b;
    private bool _field_c;

    public Class_t()
    {
        WaitCallback callBack = null;
        this._field_a = new Queue<Class_a>(0x400);
        if (callBack == null)
        {
            callBack = new WaitCallback(this.a);
        }
        ThreadPool.QueueUserWorkItem(callBack);
    }

    protected Class_a a()
    {
        lock (this._field_a)
        {
            if (this._field_a.Count > 0)
            {
                return this._field_a.Dequeue();
            }
            return null;
        }
    }

    public void a(Class_a A_0)
    {
        lock (this._field_a)
        {
            this._field_a.Enqueue(A_0);
        }
    }

    [CompilerGenerated]
    private void a(object A_0)
    {
        this.d();
    }

    protected virtual void b()
    {
        this._field_b = this.a();
        if (this._field_b != null)
        {
            try
            {
                TcpChannel channel = this._field_b.b();
                if (channel.Socket != null)
                {
                    try
                    {
                        channel._field_r._field_a.SetInfo(this._field_b.Buffer, 0, this._field_b.BytesTransferred);
                        channel.a(channel._field_r);
                    }
                    catch (Exception exception)
                    {
                        ChannelErrorEventArgs e = new ChannelErrorEventArgs {
                            Channel = channel,
                            Exception = exception
                        };
                        channel.CallChannelError(e);
                    }
                    finally
                    {
                        this._field_b.d();
                    }
                }
            }
            catch
            {
            }
        }
        else
        {
            Thread.Sleep(TcpUtils.SocketDespatchSleepTime);
        }
    }

    public int c()
    {
        return this._field_a.Count;
    }

    public virtual void d()
    {
        if (!TcpUtils.ExecutionContext)
        {
            ExecutionContext.SuppressFlow();
        }
        while (!this._field_c)
        {
            this.b();
        }
    }

    public void e()
    {
        lock (this)
        {
            if (!this._field_c)
            {
                while (this._field_a.Count > 0)
                {
                    this._field_a.Dequeue().Dispose();
                }
                this._field_c = true;
            }
        }
    }
}

